package com.etung.iot.demo.runner;

import com.etung.iot.demo.modles.RealData;
import com.etung.iot.demo.modles.StorageEntity;
import com.etung.iot.demo.storage.StorageFactory;
import com.etung.iot.demo.storage.StorageRuleUtil;
import com.etung.iot.demo.storage.conTriggerStorage.PreConditionStorageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.LinkedBlockingQueue;

@Component
@Slf4j
public class StorageRunner implements Runnable{
    public static LinkedBlockingQueue queue = new LinkedBlockingQueue<RealData>();
    @Autowired
    private StorageRuleUtil storageRuleUtil;
    @Autowired
    private PreConditionStorageUtil preConditionStorageUtil;

    @Override
    public void run() {
        log.info("StorageRunner-------Thread:" + Thread.currentThread().getName());
        preConditionStorageUtil.loadChangeTypeList();
        while (true){
            RealData realData = null;
            try {
                realData = (RealData) queue.take();
                StorageEntity storageRule = storageRuleUtil.getStorageRule(realData.getImei(), realData.getVarId());
                if (storageRule != null)
                    StorageFactory.getStorageHandler(storageRule.getType()).handler(realData);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
